package com.yc.video.tool;

import ohos.app.Context;
import ohos.net.NetCapabilities;
import ohos.net.NetHandle;
import ohos.net.NetManager;

/**
 * 网络状态监听工具类
 *
 * @since 2021-05-10
 */
public class NetworkUtils {
    /**
     * NO_NETWORK
     */
    public static final int NO_NETWORK = 0;
    /**
     * NETWORK_CLOSED
     */
    public static final int NETWORK_CLOSED = 1;
    /**
     * NETWORK_ETHERNET
     */
    public static final int NETWORK_ETHERNET = 2;
    /**
     * NETWORK_WIFI
     */
    public static final int NETWORK_WIFI = 3;
    /**
     * NETWORK_MOBILE
     */
    public static final int NETWORK_MOBILE = 4;
    /**
     * NETWORK_UNKNOWN
     */
    public static final int NETWORK_UNKNOWN = -1;

    /**
     * 标记当前网络状态，分别是：移动数据、Wifi、未连接、网络状态已公布
     *
     * @since 2021-05-10
     */
    public enum State {
        MOBILE, WIFI, UN_CONNECTED, PUBLISHED
    }

    /**
     * 为了避免因多次接收到广播反复提醒的情况而设置的标志位，用于缓存收到新的广播前的网络状态
     */
    private static State tempState;

    /**
     * 获取当前网络连接状态
     *
     * @param context Context
     * @return 网络状态
     */
    public static State getConnectState(Context context) {
        NetManager manager = NetManager.getInstance(context);
        NetCapabilities netCapabilities = manager.getNetCapabilities(manager.getDefaultNet()); // 获取NetCapabilities对象
        State state = State.UN_CONNECTED;
        if (netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED)) { // 网络已经连接,并且可以上网
            if (isMobileConnected(context)) {
                state = State.MOBILE;
            } else if (isWifiConnected(context)) {
                state = State.WIFI;
            }
        }
        if (state.equals(tempState)) {
            return State.PUBLISHED;
        }
        tempState = state;
        return state;
    }

    private static boolean isMobileConnected(Context context) {
        return isConnected(context, 0);
    }

    private static boolean isWifiConnected(Context context) {
        return isConnected(context, 1);
    }

    private static boolean isConnected(Context context, int type) {
        NetHandle[] networks = new NetHandle[0];
        NetManager manager = NetManager.getInstance(context);
        if (manager != null) {
            networks = manager.getAllNets(); // 获取已激活的网络列表
        }
        for (NetHandle network : networks) {
            NetCapabilities netCapabilities = manager.getNetCapabilities(network);
            if (netCapabilities.hasCap(type)) {
                return netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED);
            }
        }
        return false;
    }

    /**
     * 判断当前网络类型
     *
     * @param context context
     * @return return
     */
    public static int getNetworkType(Context context) {
        NetManager connectMgr = NetManager.getInstance(context);
        if (connectMgr == null) {
            return NO_NETWORK;
        }
        if (!connectMgr.hasDefaultNet()) {
            // 没有任何网络
            return NO_NETWORK;
        }
        NetHandle networkInfo = connectMgr.getDefaultNet(); // 获取默认的激活网络
        if (networkInfo == null) {
            // 网络断开或关闭
            return NETWORK_CLOSED;
        }
        NetCapabilities netCapabilities = connectMgr.getNetCapabilities(networkInfo); // 可以获取到NetHandle的对象
        // 表示有网NET_CAPABILITY_VALIDATED能用
        if (netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED)) {
            if (netCapabilities.hasCap(NetCapabilities.BEARER_ETHERNET)) {
                // 以太网网络
                return NETWORK_ETHERNET;
            } else if (netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI)
                || netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI_AWARE)) {
                // wifi网络，当激活时，默认情况下，所有的数据流量将使用此连接
                return NETWORK_WIFI;
            } else if (netCapabilities.hasBearer(NetCapabilities.BEARER_CELLULAR)) {
                // 移动数据连接,不能与连接共存,如果wifi打开，则自动关闭
                return NETWORK_MOBILE;
            }
        }
        return NETWORK_UNKNOWN; // 未知网络
    }
}
